package tt.dz.appaction.base;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import tt.dz.dao.pro.UserMemberDao;
import tt.dz.entity.UserMember;

/**
 * 拦截器
 * @author Xielf
 */
public class HandlerIntercepter implements HandlerInterceptor {
	
	/**用户信息Dao注入（使用Service接口待后期商量）*/
	@Resource
	UserMemberDao<UserMember> userMemberDao;
	
	private static final Logger log = Logger.getLogger(HandlerIntercepter.class);

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object obj, Exception exception)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object obj, ModelAndView modelAndView) throws Exception {
		log.info("HandlerIntercepter post");
	}

	/**
	 * 控制器处理前组要处理的方法（更好的方式是使用AOP，待学习和练习）
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object arg2) throws Exception {
		//获取token
		String token = request.getParameter("token");
		//获取用户ID
		String userid = request.getParameter("userid");
		Boolean bool = false;
		//token验证前提
		if(token!=null && !"".equals(token) && userid!=null && !"".equals(userid)){
			bool = userMemberDao.validUserToken(token, userid);
		}
		//TOKEN验证通过
		if(bool==true){
			System.out.println("token valid success");
		}
		log.info("HandlerIntercepter pre.");
		//目前默认返回验证拦截通过
		return true;
	}
}
